package net.expectx.pay.controller.system;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import net.expectx.pay.dao.bean.*;
import net.expectx.pay.rpc.api.SystemSettingService;
import net.expectx.pay.rpc.api.SystemTypeGroupService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseConstants;
import net.expectx.common.single.base.BaseResult;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.common.AdminResult;
import net.expectx.pay.common.util.ExpectXUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * @author Rui
 * 2018.9.14
 */
@Controller
@RequestMapping(value = "/manager/system/systemSetting")
public class SystemSettingController {
    private static final Logger LOGGER = Logger.getLogger(SystemSettingController.class);
    @Autowired
    private SystemSettingService systemSettingService;
    @Autowired
    SystemTypeGroupService systemTypeGroupService;
    @ApiOperation(value = "字典主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap modelMap, HttpServletRequest request) {
        SystemSettingExample systemSettingExample = new SystemSettingExample();
        SystemTypeGroupExample systemTypeGroupExample = new SystemTypeGroupExample();
        SystemSettingExample.Criteria criteria = systemSettingExample.createCriteria();
        SystemTypeGroupExample.Criteria criteria1 = systemTypeGroupExample.createCriteria();
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria1.andTypeGroupCodeEqualTo("web_site");
        List<SystemSetting> systemSettings = systemSettingService.selectByExample(systemSettingExample);
        SystemTypeGroup systemTypeGroup = systemTypeGroupService.selectByExample(systemTypeGroupExample).get(0);
        request.getSession().setAttribute("systemSettings",systemSettings);
        request.setAttribute("systemSetting", systemSettings);
        request.setAttribute("systemTypes", systemTypeGroup.getSystemTypes());
        return "system/systemSetting/index";
    }

    @ApiOperation(value = "添加设置")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(SystemSetting systemSetting, HttpServletRequest request) {
        systemSetting.setGmtCreate(System.currentTimeMillis());
        systemSetting.setGmtModified(System.currentTimeMillis());
        systemSetting.setIsDeleted(BaseConstants.NO);
        systemSetting.setOrders(Long.parseLong("0"));
        request.getSession().setAttribute("attrClass", systemSetting.getAttrClass());
        request.getSession().setAttribute("num", "0");
        return systemSettingService.insertSelective(systemSetting);
    }

    @ApiOperation(value = "添加系统值")
    @RequestMapping(value = "/createValue", method = RequestMethod.POST)
    @ResponseBody
    public Object createValue(String systemSettingValue) {
        JSONObject json = JSONObject.parseObject(systemSettingValue);
        Set keySet = json.keySet();
        json.remove("file");
        BaseResult baseResult =null;
        for (Object key : keySet) {
            String attrValue = json.getString(key.toString());
            if (key.toString().contains("setting")) {
                key = key.toString().substring(7);
            }
            Long id = Long.parseLong(key.toString());
            SystemSetting systemSetting = systemSettingService.selectByPrimaryKey(id);
            systemSetting.setAttrValue(attrValue);
             baseResult=    systemSettingService.updateByPrimaryKeySelective(systemSetting);
        }
        return baseResult;
    }

    @ApiOperation(value = "删除系统")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(String id) {
        SystemSettingExample systemSettingExample=new SystemSettingExample();
        systemSettingExample.createCriteria().andIdIn(ExpectXUtil.toList(id,","));
        BaseResult baseResult=systemSettingService.physicalRemoveByExample(AdminConstants.YES,systemSettingExample);
        return new AdminResult(baseResult);
    }

    @ApiOperation(value = "修改系统设置")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, HttpServletRequest request) {
        SystemSetting systemSetting = systemSettingService.selectByPrimaryKey(id);
        request.getSession().setAttribute("system", systemSetting);
        request.getSession().setAttribute("num", "0");
        request.getSession().setAttribute("attrClass", systemSetting.getAttrClass());
        return new AdminResult(true);
    }

    @ApiOperation(value = "修改系统设置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(SystemSetting systemSetting) {
        systemSetting.setGmtModified(System.currentTimeMillis());
        return systemSettingService.updateByPrimaryKeySelective(systemSetting);
    }





}

